#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define GZIP_MAGIC "\037\213"
#define RW_USER (S_IRUSR|S_IWUSR)
int   outcnt = 0;
int ofd;
char outbuf[100];
int bytes_out;
void write_buf(fd, buf, cnt)
    int       fd;
    char *    buf;
    unsigned int cnt;
{
    unsigned int  n;

    while ((n = write(fd, buf, cnt)) != cnt) {
	if (n == (unsigned int)(-1)) {
          printf("error!!!");
	}
	cnt -= n;
	buf = (void *)((char*)buf+n);
    }
}

void flush_outbuf()
{
    if (outcnt == 0) return;

    write_buf(ofd, (char *)outbuf, outcnt);
    bytes_out += (unsigned long)outcnt;
    outcnt = 0;
}

void put_byte(char s)
{
  outbuf[outcnt++] = (unsigned char) s;
  if (outcnt == 100) {
    flush_outbuf();
  }
}

int main(int argc,char **argv)
{
  unsigned char flags = 0;
  unsigned char attr = 0;
  unsigned char deflate_flags = 0;
  int ifd;
  int ofd;

  int DEFLATED = 8;
  int method = 8;
  int flags = 8;
  put_byte(GZIP_MAGIC[0]);
  put_byte(GZIP_MAGIC[1]);
  put_byte(DEFLATED);
  if ((ofd = open("test.c.gz", O_WRONLY,RW_USER)) == NULL)
    {
      exit(1);
    }
  put_byte(flags);
  put_long(time_stamp);
  write_buf(ofd, (char *)outbuf, outcnt);
  
  return 0;
}
